import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/',
    component: ()=> import('../views/Home'),
    children:[
      {
        path: '/home',
        component: ()=> import('../views/homePage')
      },
      {
        path: '/baike',
        component: ()=> import('../views/baike'),
        redirect: '/baike/p1',
        children: [
          {
            path: 'p1',
            component: ()=> import('../views/baike/BaikeP1')
          },
          {
            path: 'p2',
            component: ()=> import('../views/baike/BaikeP2')
          },
          {
            path: 'p3',
            component: ()=> import('../views/baike/BaikeP3')
          },
          {
            path: 'p4',
            component: ()=> import('../views/baike/BaikeP4')
          },
          {
            path: 'p5',
            component: ()=> import('../views/baike/BaikeP5')
          },
          {
            path: 'p6',
            component: ()=> import('../views/baike/BaikeP6')
          },

        ]
      },
      {
        path: '/mall',
        component: ()=> import('../views/mall'),
        redirect: '/mall/goodList/0',
        children:[
          {
            path: 'goodList/:id',
            component: ()=> import('../views/mall/GoodList')
          }
        ]
      },
      {
        path: '/cart',
        component: ()=> import('../views/cart')
      },
      {
        path: '/me',
        component: ()=> import('../views/me')
      }
    ]
  },
  {
    path: '/details',
    component: ()=> import('../views/mall/GoodDetails')
  },
  {
    path: '/buy',
    component: ()=> import('../views/mall/Buy')
  },
  {
    path: '/profile',
    component: ()=> import('../views/me/Profile')
  },
  {
    path: '/address',
    component: ()=> import('../views/me/AdressEdit')
  },
  {
    path: "/gift",
    component: ()=> import('../views/mall/Gift')
  },
  {
    path: "/custom",
    component: ()=> import('../views/mall/CustomMade')
  },
  {
    path: '/login',
    component: ()=> import('../views/login/Login')
  },
  {
    path: '/register',
    component: ()=> import('../views/login/Register')
  }
]

const router = new VueRouter({
  routes
})
//解决不同页面跳转高度异常问题
router.beforeEach((to, from, next) => {
  window.scroll(0, 0);
  next();
});


import { Toast } from 'vant';
//登录路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/me' || to.path== '/cart' || to.path == '/buy'){
    if(store.state.isLogin){
      next();
    }else{
      Toast('请先登录!');
      next('/login');
    }
  }else{
    next();
  }
})

export default router
